﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 初始配置时的Role信息
/// </summary>
[System.Serializable]
public class ScriptObjectRole : ScriptableObject
{
	public string RoleModelName;
	public int DefaultStage = 0;
	public List<RoleStage> StageInfor = new List<RoleStage>();

	[System.Serializable]
	public class RoleStage{
		/// <summary>
		/// 阶段ID
		/// </summary>
		public int StageIndex;
		/// <summary>
		/// 当前阶段的显示图片
		/// </summary>
		public Texture2D StageTexture;
		/// <summary>
		/// 当前阶段的Size大小
		/// </summary>
		public Vector3 StageSize;
		/// <summary>
		/// 当前阶段的最低达到分数
		/// </summary>
		public int StageMinScore;
		/// <summary>
		/// 当前阶段的速度
		/// </summary>
		public float StageSpeed;

		public ColliderType ColliderType;
		public List<Vector3> ColliderPoint;
		public Vector2 ColliderCenter;
		public Vector2 ColliderSize;
		public float ColliderRadius;

		public RoleStage Copy()
		{
			return new RoleStage()
			{
				StageIndex = StageIndex,
				StageTexture = StageTexture,
				StageSize = StageSize,
				StageMinScore = StageMinScore,
				StageSpeed = StageSpeed,
				ColliderType = ColliderType,
				ColliderPoint = ColliderPoint,
				ColliderCenter = ColliderCenter,
				ColliderSize = ColliderSize,
				ColliderRadius = ColliderRadius,
			};
		}
	}
	public enum ColliderType
	{
		Box = 0,
		Circle = 1,
		Capsule = 2,
		Polygon = 3,
	}
}

[System.Serializable]
public class RoleAtrEntity
{
	//配置信息
	public ScriptObjectRole so_Role;

	protected int currentSorce = 0;
	protected int currentStage = 0;

	protected List<EffectFactorBase> factors;
	protected List<Skill> skills;

	public int CurrentScore
	{
		get => currentSorce;
		set
		{
			int temp = value;

			while (currentStage + 1 < so_Role.StageInfor.Count && temp >= so_Role.StageInfor[currentStage + 1].StageMinScore)
			{
				currentStage += 1;
			}
			currentSorce = temp;
		}
	}
	public int CurrentStage => currentStage;
	public float CurrentSpeed
	{
		get {
			float baseSpeed = so_Role.StageInfor[currentStage].StageSpeed;

			return (baseSpeed + SpeedPlus) * SpeedMultiPos * SpeedMultiNeg;
		}
	}
	public float SpeedPlus { get; set; } = 0;
	public float SpeedMultiPos { get; set; } = 1;
	public float SpeedMultiNeg { get; set; } = 1;

	public RoleAtrEntity(ScriptObjectRole so)
	{
		factors = new List<EffectFactorBase>();
		if (null != so)
		{
			this.so_Role = so;
			InitRoleInfor();
		}
	}

	#region Factor
	/// <summary>
	/// Tick All TimeAboutFactor
	/// </summary>
	/// <param name="deltaTime"></param>
	virtual public void UpdateFactor(float deltaTime)
	{
		//factors.ForEach((fac)=> fac.Tick(deltaTime));
		for (int index = factors.Count - 1; index >= 0; index--)
		{
			factors[index].Tick(this,deltaTime);
			//if (factors[index].IsObsolete == true) factors.Remove(factors[index]);
		}

		//List<EffectFactorBase> temp;
		//foreach (var tagFac in factors)
		//{
		//	temp = tagFac.Value; 
		//	for (int index = temp.Count -1; index >= 0; index--)
		//	{
		//		temp[index].Tick(deltaTime);
		//		if (temp[index].IsObsolete == true) temp.Remove(temp[index]);
		//	}
		//}

	}
	virtual public void AddFactor(EffectFactorBase factor)
	{
		EffectFactorBase lastFactor;
		if (null != (lastFactor = factors.Find(x => x.Factor == factor.Factor)))
		{
			lastFactor.ReFresh();
			//lastFactor.EndEffect(this);
			//factors.Remove(lastFactor);
		}
		else
		{
			factor.StartEffect(this);
			factors.Add(factor);
		}

	}
	virtual public void RemoveFactor(EffectFactorBase factor)
	{
		factor.EndEffect(this);
		factors.Remove(factor);
	}
	#endregion

	#region Skill
	public void AddSkill(Skill skill)
	{
		//不可添加重复的技能
		if (null == skills.Find(x => x.SkillAtr.SkillTag == skill.SkillAtr.SkillTag))
			skills.Add(skill);
	}

	public void RemoveSkill(Skill skill)
	{
		skills.Remove(skill);
	}

	public bool CheckSkillVaild(Skill skill)
	{
		if (!skills.Contains(skill) && skill.IsActive)
			return true;
		else
			return false;
	}
	#endregion

	protected virtual void InitRoleInfor()
	{
		//Clear Cur Data
		currentSorce = 0;
		currentStage = 0;

		factors.Clear();
	}
}